<?php
namespace app\pay\controller\juyin;

class Daifuyue extends Base
{
    // 代付查询余额
    public function index() {
        $data = input('post.');     // 获取客户端POST过来的参数
        // 验证签名
        $res = $this->verifySign($data, true);
        if($res) {
            $this->Request();
            // 组装POST到聚银的数据
            $postToJuyin = array(
                'memberid'    => $this->cfg->C('merchantId', false), // 更改商户号为聚银商户号
                'account'     => $data['account'],
            );
            /*
             * 1.将订单信息保存到数据库中
             * 2.验证聚银签名并向聚银官方接口发起POST请求
             */
            $ret = $this->requestData($postToJuyin, 'daifuYuE', true);
            // 接收聚银返回的数据并组装
            $result = $this->responseData($data['memberid'], $ret);
            return $result;
        } else {
            return $res;
        }
    }
    
    // 接收聚银返回的数据并组装
    public function responseData($merchantId, $data) {
        $retData = json_decode($data, true);
        if($retData['code'] == '11') {
            // 数据组装
            $result = array(
                'code'      => $retData['code'],
                'djamount'  => $retData['djamount'],
                'account'   => $retData['account'],
                'memberno'  => $merchantId,
                'amount'    => $retData['amount'],
                'msg'       => $retData['msg'],
            );
            // 生成新的签名
            $newJuyinSignVal = $this->payRequest->createJuyinSign($result, true);
            $result['signature'] = $newJuyinSignVal;
        } else {
            $result = array(
                'code' => $retData['code'],
                'msg'  => $retData['msg']
            );
        }
        return json_encode($result, true);
    }
}

